package com.baofu.paycore.service.validate.impl.transfer;

import com.baofu.paycore.common.enums.FlagEnum;
import com.baofu.paycore.common.enums.RedisKeyEnum;
import com.baofu.paycore.common.exception.PayCoreBizException;
import com.baofu.paycore.manager.cache.RedisLockManager;
import com.baofu.paycore.manager.model.TransferBO;
import com.baofu.paycore.manager.storer.TransferOrderRepository;
import com.baofu.paycore.service.facade.enums.PayCoreErrorCode;
import com.baofu.paycore.service.facade.model.transfer.CancelTransferDTO;
import com.baofu.paycore.service.validate.AbstractPayValidate;
import com.google.common.base.Objects;
import com.system.commons.utils.ParamValidate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Description  :转账撤销校验
 *
 * Author       :LZQ
 * Date         :2016.12.01
 */
@Slf4j
@Service
public class CancelTransferValidate extends AbstractPayValidate<CancelTransferDTO> {

    /**
     * 转账仓储服务
     */
    @Autowired
    private TransferOrderRepository transferOrderRepository;

    /**
     * 订单锁服务
     */
    @Autowired
    private RedisLockManager lockManager;

    /**
     * 参数基本校验 格式及非空校验
     * @param t     被校验对象
     */
    public TransferBO validate(CancelTransferDTO t){
        super.paramsValidate(t);
        ParamValidate.validateNull(t.getDescription(),PayCoreErrorCode.DESCRIPTION_CAN_NOT_BE_NULL);

        TransferBO transferBO = transferOrderRepository.load(t.getOrigRequestBizNo(),t.getOrigRequestOrderNo(),
                t.getOrigRequestSystem());
        if(transferBO == null){
            log.error("原订单不存在:{},{}",t.getOrigRequestBizNo(),t.getOrigRequestOrderNo());
            throw new PayCoreBizException(PayCoreErrorCode.OLD_ORDER_IS_NOT_EXISTED);
        }

        validateOrderStatus(transferBO.getStatus());
        amtValidate(t.getAmt(),transferBO.getAmt());

        if(Objects.equal(FlagEnum.TRUE.getCode(),transferBO.getCancelFlag())){
            log.error("原单:{},{}已撤销,不能重复撤销:{}",t.getOrigRequestBizNo(),t.getOrigRequestOrderNo(),
                    transferBO.getCancelFlag());
            throw new PayCoreBizException(PayCoreErrorCode.RE_CANCEL_OR_EC);
        }
        return transferBO;
    }

    /**
     * 锁定支付订单
     *
     * @param t     被校验对象
     */
    public boolean lock(CancelTransferDTO t){

        Boolean cancelFlag = lockManager.lock(RedisKeyEnum.PAY_SERVICE,t.getRequestBizNo(),t.getRequestOrderNo());
        Boolean origFlag = lockManager.lock(RedisKeyEnum.PAY_SERVICE,t.getOrigRequestBizNo(),t.getOrigRequestOrderNo());

        return cancelFlag && origFlag;
    }

    /**
     * 释放订单锁
     *
     * @param t     被校验对象
     */
    public void release(CancelTransferDTO t, boolean flag){
        lockManager.release(RedisKeyEnum.PAY_SERVICE,t.getRequestBizNo(),t.getRequestOrderNo());
        lockManager.release(RedisKeyEnum.PAY_SERVICE,t.getOrigRequestBizNo(),t.getOrigRequestOrderNo());
    }
}
